package com.song.dp.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.song.dp.entity.TbBlog;
import com.song.dp.entity.TbUser;
import com.song.dp.service.ITbBlogService;
import com.song.dp.service.ITbUserService;
import com.song.dp.utils.R;
import com.song.dp.utils.Result;
import com.song.dp.utils.SystemConstants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tonysong
 * @since 2024-08-08
 */
@RestController
@RequestMapping("/blog")
@Api(value = "blog",tags = "博客接口")
public class TbBlogController {

    @Resource
    private ITbBlogService blogService;

    @Resource
    private ITbUserService userService;
    @GetMapping("/hot")
    @ApiOperation(value = "blog",notes = "热门博客")
    public Result hot(@RequestParam(name = "current",defaultValue = "1") int current){
       //对blog表进行分页展示
        Page<TbBlog> page = this.blogService.query().orderByAsc("liked")
                .page(new Page<>(current, SystemConstants.MAX_PAGE_SIZE));
        //获取分页数据
        List<TbBlog> records = page.getRecords();
        //查询每个用户的名称和头像
        records.forEach(blog->{
            Long userId = blog.getUserId();
            TbUser user = this.userService.getById(userId);
            blog.setName(user.getNickName());
            blog.setIcon(user.getIcon());
        });
        return Result.ok(records);
    }
}
